﻿@using Fap.AspNetCore.Controls
@model Fap.AspNetCore.ViewModel.JqGridViewModel
@using Fap.AspNetCore.Controls.JqGrid
@{
    IEnumerable<Fap.Core.Infrastructure.Metadata.FapDict> wfInstanceStatus = _dbContext.Dictionarys("WfProcessInstanceState").OrderBy(d => d.SortBy);
    //增加操作列
    Column column = new Column("oper");
    column.SetLabel("操作");
    column.SetSearch(false);
    column.SetSortable(false);
    column.SetWidth(140);
}
<title><fap-multilang lang-key="workflow_monitoring" default-content="流程监控"></fap-multilang></title>

<div class="row">

    <div class="col-xs-12 col-sm-12">
        <input type="hidden" id="userId" value="@ViewBag.CurrenUserId" />

        <div class="widget-box">
            <div class="widget-header widget-header-flat">
                <h4 class="widget-title"><fap-multilang lang-key="workflow" default-content="业务流程"></fap-multilang></h4>

                <div class="widget-toolbar">
                    <a href="#" data-action="fullscreen" class="orange2">
                        <i class="ace-icon fa fa-expand"></i>
                    </a>
                </div>
                <div class="widget-toolbar">
                    <select class="chosen-select form-control" style="width:300px" id="processState" multi-placeholder="流程状态...">
                        <option value="">  </option>
                        @foreach (var category in wfInstanceStatus)
                        {
                            <option value="@category.Code">@category.Name</option>
                        }
                    </select>
             
                </div>
            </div>
            <div class="widget-body">          
                <div class="widget-main">
                    <fap-grid id="process" attach-column="column" grid-model="Model" wrapper="widget-main" register-authority="false" shrink-fit="false" on-grid-complete="initMenu"></fap-grid>
                </div>
            </div>
        </div>
    </div>

</div>

<script>
    function initMenu() {
        var ids = jQuery("#grid-process").jqGrid('getDataIDs');
        for (var i = 0; i < ids.length; i++) {
            var de = '';
            var cl = ids[i];
            var ret = jQuery('#grid-process').jqGrid('getRowData', cl);
            var fid = ret.Fid;
            var status = ret.ProcessState;
            var processUid = ret.ProcessUid;
            var billUid = ret.BillUid;
            var title = ret.ProcessName;
            if (status === 'Running') {
                //运行
                de += "<a  href='javascript:void(0)' onclick='suspendProcess(\"" + fid + "\")'>挂起</a>  ";
            } else if (status === 'Suspended') {
                //挂起
                de += "<a  href='javascript:void(0)' onclick='endProcess(\"" + fid + "\")'>终止</a>  ";
                de += "<a  href='javascript:void(0)' onclick='restoreProcess(\"" + fid + "\")'>恢复</a>  ";
            }
            de += "<a  href='javascript:void(0)' onclick='workflowChart(\""+title+"\",\"" + processUid + "\",\"" + billUid + "\")'>流程图</a>  ";

            //de += "<a  href='javascript:void(0)' onclick='viewBill(\"" + fid + "\",\"" + name + "\")'>查看</a> ";

            jQuery('#grid-process').jqGrid('setRowData', ids[i], { oper: de });
        }
    }
    function suspendProcess(processInsFid) {
        layer.prompt({ title: '请填写挂起原因，并确认', formType: 2 }, function (text, index) {
            layer.close(index);
            $.post('@Url.Content("/Workflow/Api/SuspendProcess")', { processInsUid: processInsFid, comment: text }, function (rv) {
                if (rv.success) {
                    $.msg(rv.msg);
                    refreshGrid();
                } else {
                    bootbox.alert(rv.msg);
                }
            });
        });
    }
    function endProcess(processInsFid) {
        layer.prompt({ title: '请填写终止原因，并确认', formType: 2 }, function (text, index) {
            layer.close(index);
            $.post('@Url.Content("/Workflow/Api/EndProcess")',
                { processInsUid: processInsFid, comment: text },
                function (rv) {
                    if (rv.success) {
                        $.msg(rv.msg);
                        refreshGrid();
                    } else {
                        bootbox.alert(rv.msg);
                    }
                });
        });
    }
    function restoreProcess(processInsFid) {
        layer.prompt({ title: '请填写恢复原因，并确认', formType: 2 }, function (text, index) {
            layer.close(index);
            $.post('@Url.Content("/Workflow/Api/ResumeProcess")',
                { processInsUid: processInsFid, comment: text },
                function (rv) {
                    if (rv.success) {
                        $.msg(rv.msg);
                        refreshGrid();
                    } else {
                        bootbox.alert(rv.msg);
                    }
                });
        });
    }
      /**
     * 流程图
     * */
    function workflowChart(title,processUid,billUid) {
        var openUrl = $.randomUrl(basePath + "/Workflow/Business/FlowChart?processUid=" + processUid + "&billUid=" + billUid);
        var index = layer.open({
            type: 2,
            title: '"'+title+'"申请单',
            shadeClose: true,
            shade: 0.8,
            maxmin: true, //开启最大化最小化按钮
            skin: 'billskin',
            btnAlign: 'c',
            area: ['893px', '600px'],
            content: openUrl,
            btn: ['关闭'],
            yes: function (index, layero) {
                layer.close(index);
            }
            , cancel: function () {
                //右上角关闭回调

                //return false 开启该代码可禁止点击该按钮关闭
            }
        });
        layer.full(index);
    }

    //刷新流程列表
    function refreshGrid() {
        var c = $("#processState").val();
        if (c !== "") {
            var filter = '{"groupOp":"AND","rules":[{"field":"ProcessState","op":"eq","data":"' + c + '"}]}'

            reloadGrid("grid-process", { filters: filter })
        } else {
            reloadGrid("grid-process");
        }
    }       
    var scripts = [null, null]
    $('.page-content-area').ace_ajax('loadScripts', scripts, function () {
        jQuery(function ($) {
            $(document).one('ajaxloadstart.page', function (e) {
               
            })          
            $('.chosen-select').chosen({ allow_single_deselect: true, search_contains: true }).on("change", function () {
                refreshGrid();
            });            
        });
    })
</script>
